package secondBrush.array;

public class E04Leetcode209 {
    public static int minSubArrayLen(int target, int[] nums) {
        int sum = 0;
        int result = Integer.MAX_VALUE;
        int temp = 0;
        int length = nums.length;
        for (int i = 0; i < length; i++) {
            sum = 0;
            temp = 0;
            for (int j = i; j < length; j++) {
                sum += nums[j];
                temp++;

                if (sum >= target) {
                    result = Math.min(temp, result);

                    break;
                }
            }
        }
        return result == Integer.MAX_VALUE ? 0 : result;

    }

    public static int minSubArrayLen2(int target, int[] nums) {
        int length = nums.length;
        int sum = 0;
        int left = 0;
        int result = Integer.MAX_VALUE;
        for (int right = 0; right < length; right++) {
            sum += nums[right];
            while (sum >= target) {
                result = Math.min(result, right - left + 1);
                sum -= nums[left++];
            }
        }
        return result == Integer.MAX_VALUE ? 0 : result;
    }





    public static void main(String[] args) {
        int[] nums = {1,1,1,1,1,1,1,1};
        int target = 11;
        System.out.println(minSubArrayLen(target, nums));

    }
}
